﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RecipeRecommender
{
    class SimilarityUtil
    {

        public static double getSimilarityScore(int? input, int? examined, int max)
        {
            if (input == null) return 0;
            else return 1 - (Math.Abs((double)examined - (double)input) / (double) max);
        }

        public static double getSimilarityScore(bool? input, bool? examined)
        {
            if (input == examined) return 1;
            else return 0;
        }

        public static double getSimilarityScore(string input, string examined)
        {
            if (input == null || input.Equals("")) return 0;

           string [] inputWords =  input.ToLower().Split();
           string[] examinedWords = examined.ToLower().Split();

           List<string> inputTrigramList = new List<string>();
           List<string> examinedTrigramList = new List<string>();

           foreach (string word in inputWords)
           {
               List<string> trigramWords = generateTrigram(word);
               inputTrigramList.AddRange(trigramWords);
           }

           foreach (string word in examinedWords)
           {
               List<string> trigramWords = generateTrigram(word);
               examinedTrigramList.AddRange(trigramWords);
           }

           double intersectionCount = SimilarityUtil.getIntersection(inputTrigramList, examinedTrigramList);
           //double unionCount = SimilarityUtil.getUnion(inputTrigramList, examinedTrigramList, intersectionCount);
           return intersectionCount / inputTrigramList.Count;
          // return intersectionCount / unionCount;

        }

        private static List<string> generateTrigram(string word)
        {
            List<string> trigramWords = new List<string>();
            if (word.Length<=3) {
                trigramWords.Add(word);
                
            } else {

                for (int i = 0; i < word.Length - 2; i++)
                {
                    trigramWords.Add(word.Substring(i,3));
                }

            }   
            return trigramWords;
        }

        public static double getIntersection(List<string> completeInputValueList, List<string> completeExaminedValueList)
        {
            List<string> completeExaminedValueListCopy = new List<string>();
            foreach (string s in completeExaminedValueList)
                completeExaminedValueListCopy.Add(s);

            int count = 0;
            foreach (string v in completeInputValueList)
            {
                if (completeExaminedValueListCopy.Contains(v))
                {
                    completeExaminedValueListCopy.Remove(v);
                    count++;
                }
            }

            return count;
        }

        public static double getUnion(List<string> completeInputValueList, List<string> completeExaminedValueList, double intersectionCount)
        {
            // A U B = A + B - A n B
            return completeInputValueList.Count + completeExaminedValueList.Count - intersectionCount;
        }
    }
}
